package com.msh.automatic.android.control

import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.View
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.navigation.NavigationView
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.drawerlayout.widget.DrawerLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import cn.hutool.core.date.DateUtil
import com.msh.automatic.android.control.databinding.ActivityMainBinding
import com.msh.automatic.android.control.service.MqttService
import com.msh.automatic.android.control.task.TaskManager
import com.msh.automatic.android.control.util.ConfigUtil
import com.msh.automatic.android.control.util.api.TaskApi
import com.tencent.bugly.crashreport.CrashReport
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        CrashReport.initCrashReport(getApplicationContext());

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.appBarMain.toolbar)

        binding.appBarMain.fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null)
                .setAnchorView(R.id.fab).show()
        }
        val drawerLayout: DrawerLayout = binding.drawerLayout
        val navView: NavigationView = binding.navView
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_home, R.id.nav_mqtt, R.id.nav_slideshow
            ), drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
        onTask()
    }
    override fun onResume() {
        super.onResume()

        // 这里的代码每次返回 MainActivity 时都会执行
        Log.d("MainActivity", "MainActivity 重新显示")
        onTask()
        // 例如：刷新数据、更新界面、请求权限、检测状态等
    }

    fun onTask(){
        Log.d("TASK","onTask ${TaskManager.currentBaseTask}")
        TaskManager.currentBaseTask?.let {
            GlobalScope.launch {
                val (code,message,body) = TaskApi.info(it.getId())

                if(code.equals("SUCCESS")){
                    val data = body!!
                    Log.d("TASK","onTask ${code} ${message} ${data.toJSONString()}")
                    if(data.getString("status").equals("over")){
                        TaskManager.currentBaseTask = null
                    }
                }else{
                    Log.d("TASK","onTask ${code} ${message}")
                }
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
}